/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.enums.AuctionOrderEnum;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.auction.domain.AuctionOrder;
import co.yixiang.modules.auction.domain.AuctionRecord;
import co.yixiang.modules.auction.service.AuctionOrderService;
import co.yixiang.modules.auction.service.dto.AuctionOrderDto;
import co.yixiang.modules.auction.service.dto.AuctionOrderQueryCriteria;
import co.yixiang.modules.auction.service.mapper.YxAuctionOrderMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* @author yhh
* @date 2021-02-18
*/
@AllArgsConstructor
@Api(tags = "抢拍订单流水管理")
@RestController
@RequestMapping("/api/AuctionOrder")
public class AuctionOrderController {

    private final AuctionOrderService AuctionOrderService;
    private final YxAuctionOrderMapper auctionOrderMapper;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','AuctionOrder:list')")
    public void download(HttpServletResponse response, AuctionOrderQueryCriteria criteria) throws IOException {
        AuctionOrderService.download(generator.convert(AuctionOrderService.queryAll(criteria), AuctionOrderDto.class), response);
    }

    @GetMapping
    @Log("查询抢拍订单流水")
    @ApiOperation("查询抢拍订单流水")
    @PreAuthorize("@el.check('admin','AuctionOrder:list')")
    public ResponseEntity<Object> getAuctionOrders(AuctionOrderQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(AuctionOrderService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增抢拍订单流水")
    @ApiOperation("新增抢拍订单流水")
    @PreAuthorize("@el.check('admin','AuctionOrder:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody AuctionOrder resources){
        return new ResponseEntity<>(AuctionOrderService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改抢拍订单流水")
    @ApiOperation("修改抢拍订单流水")
    @PreAuthorize("@el.check('admin','AuctionOrder:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AuctionOrder resources){
        AuctionOrderService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除抢拍订单流水")
    @ApiOperation("删除抢拍订单流水")
    @PreAuthorize("@el.check('admin','AuctionOrder:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id->{
            AuctionOrderService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("发货")
    @ApiOperation("发货")
    @GetMapping("/conversion")
    public ResponseEntity<Object> conversion(@RequestParam(value = "id" ,required = false) Long id,
                                             @RequestParam(value = "express" ,required = false) String express,
                                             @RequestParam(value = "expressNumber" ,required = false) String expressNumber) {
        AuctionOrder resources = AuctionOrderService.getById(id);
        resources.setStatus(3);
        resources.setExpress(express);
        resources.setExpressNumber(expressNumber);
        AuctionOrderService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("获取枪弹人数")
    @ApiOperation("获取枪弹人数")
    @GetMapping("/getQdNum")
    public ResponseEntity getQdNum() {

        Map<String , Integer> h = new HashMap();
        //抢到单人数
        Integer qdNum = auctionOrderMapper.getQdNum(AuctionOrderEnum.TYPE_4.getValue());
        h.put("qdNum" , qdNum);
        //抢单人数
        Integer qpNum = auctionOrderMapper.getQpNum();
        h.put("qpNum" , qpNum);
        //未抢到单人数
        Integer wqdNum = auctionOrderMapper.getWqdNum();
        h.put("wqdNum" , wqdNum);
        return new ResponseEntity<>(h , HttpStatus.OK);
    }

}
